%!TEX root = ../template.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% fix-hyperref.sty
%% NOVA thesis configuration file
%%
%% Minor fixes to hyperref so that \autoref works in the proper language
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\typeout{NT FILE fix-hyperref.sty}%

\def\HyLang@portuguese{\HyLang@portuges}

\newcommand{\@nt@fixhyperref}{
  \def\HyLang@DeclareLang##1##2##3{%
    \@ifpackagewith{babel}{##1}{%
      \expandafter\HyLang@addto
          \csname extras##1\expandafter\endcsname
          \csname HyLang@##2\endcsname
      \begingroup
        \edef\x{\endgroup
          ##3%
        }%
      \x
      \@namedef{HyLang@##1@done}{}%
    }{% added check for "main=##1" (presumably Heiko can do better)
      \@ifpackagewith{babel}{main=##1}{%
        \expandafter\HyLang@addto
          \csname extras##1\expandafter\endcsname
          \csname HyLang@##2\endcsname
        \begingroup
          \edef\x{\endgroup
            ##3%
          }%
        \x
        \@namedef{HyLang@##1@done}{}%
       }{}%
    }%
    \begingroup
      \edef\x####1####2{%
        \noexpand\ifx####2\relax
          \errmessage{No definitions for language ##2' found!}%
        \noexpand\fi
        \endgroup
        \noexpand\define@key{Hyp}{##1}[]{%
          \noexpand\@ifundefined{HyLang@##1@done}{%
            \noexpand\HyLang@addto{\noexpand####1}{\noexpand####2}%
            ##3%
            \noexpand\@namedef{HyLang@##1@done}{}%
          }{}%
        }%
      }%
    \expandafter\x\csname extras##1\expandafter\endcsname
                  \csname HyLang@##2\endcsname
  }
  % repeat the known language declarations
  \HyLang@DeclareLang{english}{english}{}
  \HyLang@DeclareLang{UKenglish}{english}{}
  \HyLang@DeclareLang{british}{english}{}
  \HyLang@DeclareLang{USenglish}{english}{}
  \HyLang@DeclareLang{american}{english}{}
  \HyLang@DeclareLang{german}{german}{}
  \HyLang@DeclareLang{austrian}{german}{}
  \HyLang@DeclareLang{ngerman}{german}{}
  \HyLang@DeclareLang{naustrian}{german}{}
  \HyLang@DeclareLang{russian}{russian}{\noexpand\hypersetup{unicode}}
  \HyLang@DeclareLang{brazil}{portuguese}{}
  \HyLang@DeclareLang{brazilian}{portuguese}{}
  \HyLang@DeclareLang{portuguese}{portuguese}{}
  \HyLang@DeclareLang{spanish}{spanish}{}
  \HyLang@DeclareLang{afrikaans}{afrikaans}{}
  \HyLang@DeclareLang{french}{french}{}
  \HyLang@DeclareLang{frenchb}{french}{}
  \HyLang@DeclareLang{francais}{french}{}
  \HyLang@DeclareLang{acadian}{french}{}
  \HyLang@DeclareLang{canadien}{french}{}
  \HyLang@DeclareLang{italian}{italian}{}
  \HyLang@DeclareLang{magyar}{magyar}{}
  \HyLang@DeclareLang{hungarian}{magyar}{}
}

\AtBeginDocument{
  \RequirePackage{catoptions}
  \def\Autoref#1{%
    \begingroup
    \edef\reserved@a{\cpttrimspaces{#1}}%
    \ifcsndefTF{r@#1}{%
      \xaftercsname{\expandafter\testreftype\@fourthoffive}
        {r@\reserved@a}.\\{#1}%
    }{%
      \ref{#1}%
    }%
    \endgroup
  }
  \def\testreftype#1.#2\\#3{%
    \ifcsndefTF{#1autorefname}{%
      \def\reserved@a##1##2\@nil{%
        \uppercase{\def\ref@name{##1}}%
        \csn@edef{#1autorefname}{\ref@name##2}%
        \autoref{#3}%
      }%
      \reserved@a#1\@nil
    }{%
      \autoref{#3}%
    }%
  }
}
